<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<base th:href="@{/}"/>
<title>Insert title here</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"/>
<link rel="stylesheet" th:href="@{/css/Managerstyle.css}"/>
<script th:src="@{/js/jquery-3.4.1.min.js}"></script>
<script th:src="@{/js/bootstrap.min.js}"></script>
</head>
<body>
	<div class="contain">
		<h3>专业教学计划</h3>
		<div class="query">
			<span>查询专业教学计划</span>
			<br>
			<select id="selectAcdm"  style="width:200px">
				<option value="">请选择学院</option>
				<option th:each="acadamy:${acadamyList}" th:value="${acadamy.acdmid}"  th:text="${acadamy.acdmname}"></option>
			</select>
			<select id="selectMj"  name="majorId"  style="width:180px" >
				<option  value="">请选择专业</option>	
			</select>
			<button id="selectButton" type="button" class="btn btn-info">查询</button>
			<table class="table table-hover">
				<thead>
					<tr>
						<td width="367">学院名称</td>
						<td width="224">专业名称</td>
						<td>学期</td>
						<td>课程名称</td>
						<td width="256">操作</td>
					</tr>
				</thead>
				<tbody id = "displayTable"></tbody>
			</table>
			<br>
		<form>	
			<span>添加专业教学计划</hspan>
			<br>
			<select id="select1" name="acadamyId" style="width:200px">
				<option value="">请选择学院</option>
				<option th:each="acadamy:${acadamyList}" th:value="${acadamy.acdmid}"  th:text="${acadamy.acdmname}"></option>
			</select>
			<select id="select2"  name="majorId"  style="width:150px" >
				<option  value="">请选择专业</option>	
			</select>	
			<select id="select5"  name="termId"  style="width:150px" >
				<option  value="">请选择学期</option>	
			</select>
			<br>
			<br>
			<select id="select3" style="width:200px" >
				<option value="">请选择学院</option>
				<option th:each="acadamy:${acadamyList}" th:value="${acadamy.acdmid}"  th:text="${acadamy.acdmname}"></option>
			</select>
			<select id="select4"  name="majorId"  style="width:150px">
				<option  value="">请选择课程</option>	
			</select>
			<br>
			<br>
			<button id="addOneBtn" type="button" class="btn btn-info">添加</button>
			&nbsp;&nbsp;&nbsp;
			<span id="msg" style="color: red;"></span>
		</form>
		</div>
	</div>
	
	
	<script>
		$(document).ready(function(){
			var year;
			$("#select1").change(function(){
				var str2 = "<option  value=\"\">请选择专业</option>";
				var str3 = "<option  value=\"\">请选择学期</option>";
				
				$.ajax({
					url: 'mc/getMajorByacdmid',
					type: 'GET',
					data: {acdmid: $("#select1").val()},
					/* async: false, */
					success: function(data){
						var str1 = "";
						$("#select2").empty();
					 	$("#select2").append(str2); 
					 	$("#select5").empty();
						$("#select5").append(str3);
						$.each(data,function(index,value){
							str1 = "<option value='"+ value.mjid +"'>" + value.term.tyear + value.mjname +  "</option>";
							$("#select2").append(str1);			
						});
					},
					error: function(req,status,error){
						$("#select2").empty();
						$("#select2").append(str2);
						$("#select5").empty();
						$("#select5").append(str3);
					}
				}); 
			});
			$("#selectAcdm").change(function(){
				var str2 = "<option  value=\"\">请选择专业</option>";
				
				$.ajax({
					url: 'mc/getMajorByacdmid',
					type: 'GET',
					data: {acdmid: $("#selectAcdm").val()},
					/* async: false, */
					success: function(data){
						var str1 = "";
						$("#selectMj").empty();
					 	$("#selectMj").append(str2); 
						$.each(data,function(index,value){
							str1 = "<option value='"+ value.mjid +"'>" + value.term.tyear + value.mjname +  "</option>";
							$("#selectMj").append(str1);			
						});
					},
					error: function(req,status,error){
						$("#selectMj").empty();
						$("#selectMj").append(str2);
					}
				}); 
			});
			$("#select2").change(function(){
				var str2 = "<option  value=\"\">请选择学期</option>";
				$.ajax({
					url: 'mc/selectTermBymjid',
					type: 'GET',
					data: {mjid: $("#select2").val()},
					success: function(data){
						var str5 = "";
						$("#select5").empty();
					 	$("#select5").append(str2); 
						$.each(data,function(index,value){
							str5 = "<option value='"+ value.termId +"'>" + value.termName + "</option>";
							$("#select5").append(str5);			
						});
					},
					error: function(req,status,error){
						$("#select5").empty();
						$("#select5").append(str2);
					}
				}); 
			});
		
			
			$("#select3").change(function(){
				var str2 = "<option  value=\"\">请选择课程</option>";
				$.ajax({
					url: 'mc/selectCourseByAcadamy',
					type: 'GET',
					data: {acdmid: $("#select3").val()},
					/*  async: false,  */
					success: function(data){
						var str1 = "";
						$("#select4").empty();
					 	$("#select4").append(str2); 
					 	$.each(data,function(index,value){
							str1 = "<option value='"+ value.cid +"'>" + value.cname + "</option>";
							$("#select4").append(str1);
						});
					 	
					},
					error: function(req,status,error){
						$("#select4").empty();
						$("#select4").append(str2);
					}
				}); 
			});
			
			
			$("#addOneBtn").click(function(){
				$.ajax({
					url: 'mc/addLessionPlan',
					type: 'POST',
					data: {mjid: $("#select2").val(), termId: $("#select5").val(), cid:$("#select4").val()},
					success: function(value){
						if(value === null || value === ""){
							$("#msg").html("该课程已存在！");
						}else{
							$("#msg").html("");
							str1 = "<tr id='tr" + value.lpid + "'>" + "<td>" + value.major.acadamy.acdmname + "</td>" + "<td>" +  value.major.term.tyear + value.major.mjname + "</td>" + 
							"<td>"+	value.term.termName +"</td>" + "<td>"+ value.course.cname +"</td>" + 
							"<td>" + "<a href=\"mc/toCoursePlan?lpid="+ value.lpid +"&mjid="+ value.major.mjid + "\">" + "开课</a>" +  
							"<a href=\"javascript:deleteLessionPlan("+ value.lpid +")\">" + "删除</a>" +  
							"</td>" + 
							"</tr>";
							$("#displayTable").append(str1);
						}
					},
					error: function(req,status,error){
						alert("请填写完整信息！");
					}
				});
			});
			
			
			$("#selectButton").click(function(){
				if($("#selectMj").val()==""){
					alert("请填写完整信息！");
				}	
				
				$.ajax({
					url:"mc/selectLessionPlanBymjid",
					type:"GET",
					data:{mjid: $("#selectMj").val()},
					success:function(data){
						$("#displayTable").empty();
						$.each(data,function(index,value){
							str1 = "<tr id='tr" + value.lpid + "'>" + "<td>" + value.major.acadamy.acdmname + "</td>" + "<td>" +  value.major.term.tyear +value.major.mjname + "</td>" + 
							"<td>"+	value.term.termName +"</td>" + "<td>"+ value.course.cname +"</td>" + 
							"<td>" + "<a href=\"mc/toCoursePlan?lpid="+ value.lpid + "&mjid=" + value.major.mjid + "\">" + "开课</a>" +  
							"<a href=\"javascript:deleteLessionPlan("+ value.lpid +")\">" + "删除</a>" +  
							"</td>" + 
							"</tr>";
							$("#displayTable").append(str1);
						});
					},
					error: function(req,status,error){
					}
				});
			});

		});
	
		function deleteLessionPlan(lpid){
			$.ajax({
				url: "mc/deleteLessionPlanBylpid",
				type: "GET",
				data: {lpid: lpid},
				success:function(){
					$("#tr"+lpid).remove();
				},
				error: function(req,status,error){
				}
				
			});
		}
		
		function checkContent() {
			if($("#select1").val() === '' || $("#select2").val() === ''){
				$('#addOneBtn').attr('disabled',true);
			}else{
				$('#addOneBtn').attr('disabled',false);
			}
		};
	</script>
</body>
</html>